<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/page/inc/taglib.jsp"%>
<style>
.buttonRight {
	left: 555px;
	bottom: -18.9px;
	margin-left: 10px;
}
</style>
<div class="pageContent">
	<form method="post" action="${pageContext.request.contextPath}${requestScope.record ==null ?'/warehouse/wareOutOrder/insert' :'/warehouse/wareOutOrder/update' }" class="pageForm required-validate" onsubmit="return validateCallback(this, dialogAjaxDone);">
		<div class="pageFormContent" layoutH="56">
			<!-- 关闭窗口 -->
			<input type="hidden" name="callbackType" value="closeCurrent" />
			<!-- 隐藏表单 -->
			<c:if test="${requestScope.record !=null}">
				<input type="hidden" name="id" value="${requestScope.record.id}" />
			</c:if>
			<input type="hidden" name="csmr" value="${requestScope.record.csmr}" />
			<fieldset>
				<legend>出库单</legend>
				
				
				
				<p>
					<label>客户名称</label><input name="name" class="required" maxlength="100" type="text" value="${requestScope.record.name}"/>
				</p>
				<p>
					<label>联系方式</label><input name="phone" class="required" maxlength="100" type="text" value="${requestScope.record.phone}"/>
				</p>
				<p>
					<label>收货地址</label><input name="addr" class="required" maxlength="100" type="text" value="${requestScope.record.addr}"/>
				</p>
				<p>
					<label>物流公司</label><input name="logistics" class="required" maxlength="100" type="text" value="${requestScope.record.logistics}"/>
				</p>
				
				
				
				
				
				<p>
					<label>订单编号</label><input name="onum" class="required" maxlength="100" type="text" value="${requestScope.record.onum}" onchange="onumChange(this)" />
				</p>
				<%-- <p>
					<label>数量</label> <input name="amut" class="required" maxlength="11" type="text" value="${requestScope.record.amut}" />
				</p> --%>
				<p>
					<label>订单总额</label><input name="opcsPice" class="required" maxlength="11" type="text" value="<nf:nfmt num="${requestScope.record.opcs}"/>" />
				</p>
				<p>
					<label>配送方式</label>
					<!-- 配送方式：1-自配/物流、2-自提/送货上门 -->
					<%-- <select name="dtbt" class="combox">
						<option value="" selected>请选择</option>
						<option value="1" ${requestScope.record.dtbt==1?'selected':''}>自配/物流</option>
						<option value="2" ${requestScope.record.dtbt==2?'selected':''}>自提/送货上门</option>
					</select> --%>
					<input type="checkbox" name="dtbts" value="1" <c:if test="${fn:contains(requestScope.record.dtbt, '1') }">checked="checked"</c:if> id="match" ><label for="match" style="float: none">自配</label>
					<input type="checkbox" name="dtbts" value="2" <c:if test="${fn:contains(requestScope.record.dtbt, '2') }">checked="checked"</c:if> id="lift"><label for="lift" style="float: none">自提</label>
					<input type="checkbox" name="dtbts" value="3" <c:if test="${fn:contains(requestScope.record.dtbt, '3') }">checked="checked"</c:if> id="express"><label for="express" style="float: none">快递</label>
					<input type="checkbox" name="dtbts" value="4" <c:if test="${fn:contains(requestScope.record.dtbt, '4') }">checked="checked"</c:if> id="logistics"><label for="logistics" style="float: none">物流</label>
				</p>
				<p>
					<label>运费</label>
					<!-- 运费：1-公司出、2-代付、3-提付、4-到付 -->
					<select name="fare" class="combox">
						<option value="" selected>请选择</option>
						<option value="1" ${requestScope.record.fare==1?'selected':''}>公司出</option>
						<option value="2" ${requestScope.record.fare==2?'selected':''}>代付</option>
						<option value="3" ${requestScope.record.fare==3?'selected':''}>提付</option>
						<option value="4" ${requestScope.record.fare==4?'selected':''}>到付</option>
					</select>
				</p>
				<%-- <p>
					<label>出入库</label> <select name="type" class="combox">
						<option value="">请选择</option>
						<option value="1" ${requestScope.record.type==1?'selected':''}>出库</option>
						<option value="2" ${requestScope.record.type==2?'selected':''}>入库</option>
					</select>
				</p> --%>
			</fieldset>
			<fieldset>
				<legend>商品出库</legend>

				<div id="parameter" class="nowrap" style="margin-left: auto; margin-right: auto;">
					<a class="button buttonRight" href="javascript:addParameter();"><span>＋添加</span></a>
					<!-- 没数据是显示 -->
					<c:if test="${fn:length(requestScope.orderDetails)==0}">
						<div class="unit">
							<label style="width: 95px;">商品名称</label> <select style="width: 90px;" name="pnid" onchange="changeVal(this)">
								<option value="">请选择</option>
								<c:forEach items="${requestScope.productNorms}" var="norms">
									<option value="${norms.merchantProductNormId}">${norms.normName}</option>
								</c:forEach>
							</select> <label style="width: 65px;">仓库名称</label> <select style="width: 90px;" name="whid" onchange="changeVal(this)">
								<option value="">请选择</option>
								<c:forEach items="${requestScope.warehouses}" var="witems">
									<option value="${witems.id}">${witems.name}</option>
								</c:forEach>
							</select> 
							<label style="width: 35px;">规格</label> <input name="pack" type="text" readonly="readonly" style='background-color: #f6f6f6;' value="${item.pack}"> 
							<label style="width: 35px;">单位</label> <input name="capacity" type="text" readonly="readonly" style='background-color: #f6f6f6;' value="${item.capacity}"> 
							<label style="width: 35px;">数量</label><input type="text" name="amut" class="textInput" onchange="changeVal(this)"> 
							<a class="button buttonRight" href="javascript:void()" onclick="delParameter(this)"><span>-删除</span></a>
						</div>
					</c:if>
					
					<c:forEach items="${requestScope.orderDetails }" var="item" varStatus="status">
						<div class="unit">
							<input type="hidden" name="wioodid" value="${item.id}"> 
							<label style="width: 95px;">商品名称</label> 
							<select style="width: 90px;" name="pnid" onchange="changeVal(this)">
								<option value="">请选择</option>
								<c:forEach items="${requestScope.productNorms}" var="norms">
									<option value="${norms.merchantProductNormId}" ${item.pnid == norms.merchantProductNormId?'selected':''}>${norms.normName}</option>
								</c:forEach>
							</select> 
							<label style="width: 65px;">仓库名称</label> 
							<select style="width: 90px;" name="whid" onchange="changeVal(this)">
								<option value="">请选择</option>
								<c:forEach items="${requestScope.warehouses}" var="witems">
									<option value="${witems.id}" ${item.whid == witems.id?'selected':''}>${witems.name}</option>
								</c:forEach>
							</select> 
							<label style="width: 35px;">规格</label> <input name="pack" type="text" readonly="readonly" value="${item.pack}"> 
							<label style="width: 35px;">单位</label> <input name="capacity" type="text" readonly="readonly" value="${item.capacity}"> 
							<label style="width: 35px;">数量</label><input type="text" name="amut" class="textInput digits" value="${item.amut}" onchange="changeVal(this)"> 
							<a class="button buttonRight" href="javascript:void()" onclick="delParameter(this)"><span>-删除</span></a>
						</div>
					</c:forEach>
				</div>
			</fieldset>

		</div>
		<div class="formBar">
			<ul>
				<!--<li><a class="buttonActive" href="javascript:;"><span>保存</span></a></li>-->
				<li><div class="buttonActive">
						<div class="buttonContent">
							<button type="submit">保存</button>
						</div>
					</div></li>
				<li>
					<div class="button">
						<div class="buttonContent">
							<button type="button" class="close">取消</button>
						</div>
					</div>
				</li>
			</ul>
		</div>
	</form>
</div>
<script type="text/javascript">
	function addParameter() {
		$("#parameter").append("<div class='unit'>                                                                                                            "+
				"	<label style='width: 95px;'>商品名称</label> <select style='width: 90px;' name='pnid' onchange='changeVal(this)'>                           "+
				"		<option value=''>请选择</option>                                                                                       "+
				"		<c:forEach items='${requestScope.productNorms}' var='norms'>                                                           "+
				"			<option value='${norms.merchantProductNormId}'>${norms.normName}</option>                                          "+
				"		</c:forEach>                                                                                                           "+
				"	</select> <label style='width: 65px;'>仓库名称</label> <select style='width: 90px;' name='whid' onchange='changeVal(this)'>                     "+
				"		<option value=''>请选择</option>                                                                                       "+
				"		<c:forEach items='${requestScope.warehouses}' var='witems'>                                                            "+
				"			<option value='${witems.id}'>${witems.name}</option>                                                               "+
				"		</c:forEach>                                                                                                           "+
				"	</select>                                                                                                                  "+
				"	<label style='width: 35px;'>规格</label> <input type='text' readonly='readonly' style='background-color: #f6f6f6;' class='textInput' value='${item.pack}'  name='pack'>                      "+
				"	<label style='width: 35px;'>单位</label> <input type='text' readonly='readonly' style='background-color: #f6f6f6;' class='textInput' value='${item.capacity}' name='capacity'>                  "+
				"	<label style='width: 35px;'>数量</label><input type='text' name='amut' class='textInput' onchange='changeVal(this)'>"+
				"	<a class='button buttonRight' href='javascript:void()' onclick='delParameter(this)'><span>-删除</span></a>                 "+
				"</div>                                                                                                                        ");
	}
	function delParameter(obj) {
		$(obj).parent().remove();
	}
	function changeVal(e) {
		var pnid = $(e).parent().find("[name='pnid']").val();
		var whid = $(e).parent().find("[name='whid']").val();
		var amut = $(e).parent().find("[name='amut']").val();

		var data = {
			pnid : pnid,
			whid : whid,
			amut : amut
		};
		console.log(data);
		
		if(pnid){
			$.ajax({
				//type : form.method || 'POST',
				url : "${pageContext.request.contextPath}/merchant/merchantProductNorm/getMerchantProductNorm",
				//获取表单的内容数据  
				data : data,
				//dataType : "json",
				//cache : false,
				//执行回调函数  
				success : function(result) {
					var code = result.statusCode;
					var msg = result.message;
					if (code == 200) {
						var content = result.content;
						$(e).parent().find("[name='pack']").val(content.pack);
						$(e).parent().find("[name='capacity']").val(content.capacity);
						
					}
					if (code == 300) {
						alertMsg.warn(msg);
					}
				},
				error : function(result) {
					alert(result)
				}
			});
		}
		
		
		
		
		
		
		
		if (data.pnid && data.whid && data.amut) {
			//Ajax向后台提交数据  
			$.ajax({
				//type : form.method || 'POST',
				url : "${pageContext.request.contextPath}/warehouse/wareOutOrder/getWarning",
				//获取表单的内容数据  
				data : data,
				//dataType : "json",
				//cache : false,
				//执行回调函数  
				success : function(result) {
					var code = result.statusCode;
					var msg = result.message;
					if (code == 300) {
						alertMsg.warn(msg);
					}
				},
				error : function(result) {
					alert(result)
				}
			});
		}
	}

	function onumChange(e) {
		if (e.value) {
			var data = {
				onum : e.value
			};
			//Ajax向后台提交数据  
			$.ajax({
				//type : form.method || 'POST',
				url : "${pageContext.request.contextPath}/warehouse/wareOutOrder/onumChange",
				//获取表单的内容数据  
				data : data,
				//dataType : "json",
				//cache : false,
				//执行回调函数  
				success : function(result) {
					console.log(result);
					var code = result.statusCode;
					var msg = result.message;
					if (code == 200) {
						var content = result.content;
						var merchantOrder = content.merchantOrder;
						var orderInfo = content.orderInfo;
						if (merchantOrder) {
							$("[name='opcsPice']").val(merchantOrder.orderMoney / 100);
							$("[name='csmr']").val(merchantOrder.shopId);
						}

						if (orderInfo) {
							$("#parameter").html("<a class='button buttonRight' href='javascript:addParameter();'><span>＋添加</span></a>");
							for (var i = 0; i < orderInfo.length; i++) {
								var pack =	orderInfo[i].pack;
								var capacity =	orderInfo[i].capacity;
								if (!pack){
									pack="";
								}
								if (!capacity){
									capacity="";
								}
								$("#parameter").append(
										 "<div class='unit'>" 
										 		+ "	<input type='hidden' name='wioodid' value='"+orderInfo[i].mbid+"'> <label style='width: 95px;'>商品名称</label> <select id='pnid" + i + "' name='pnid' onchange='changeVal(this)'>    " 
										 		+ "		<option value=''>请选择</option>                                                                                                                        " 
										 		+ "		<c:forEach items='${requestScope.productNorms}' var='norms'>                                                                                            " 
										 		+ "			<option value='${norms.merchantProductNormId}' "+orderInfo[i].mbnid+" a=${norms.merchantProductNormId}>${norms.normName}</option>                 "
												+ "		</c:forEach>                                                                                                                                            " 
												+ "	</select> <label style='width: 65px;'>仓库名称</label> <select name='whid' onchange='changeVal(this)'>                                                      " 
												+ "		<option value=''>请选择</option>                                                                                                                        " 
												+ "		<c:forEach items='${requestScope.warehouses}' var='witems'>                                                                                             " 
												+ "			<option value='${witems.id}'>${witems.name}</option>                                                        " 
												+ "		</c:forEach>                                                                                                                                            "
												+ "	</select> "
												+ "	<label style='width: 35px;'>规格</label> <input type='text' readonly='readonly' style='background-color: #f6f6f6;' class='textInput' value='" + pack + "' name='pack'>                      "
												+ "	<label style='width: 35px;'>单位</label> <input type='text' readonly='readonly' style='background-color: #f6f6f6;' class='textInput' value='" + capacity + "' name='capacity'>                  "
												+ "<label style='width: 35px;'>数量</label><input type='text' name='amut' class='textInput digits' value='" + orderInfo[i].mbcount + "' onchange='changeVal(this)'>"
												+"	<a class='button buttonRight' href='javascript:void()' onclick='delParameter(this)'><span>-删除</span></a>                 "
												+ "</div>                                                                                                                                                         ");
							}
							for (var i = 0; i < orderInfo.length; i++) {
								$("#pnid" + i + " option").each(function() {
									if ($(this).val() == orderInfo[i].mbnid) {
										$(this).attr("selected", "selected");
									}
								});
							}
						}
					}
				},
				error : function(result) {
					alert(result)
				}
			});
		}
	}
</script>